package ce.com.cenewbluesdk.entity;

import java.util.Calendar;
import java.util.Locale;

import ce.com.cenewbluesdk.CEBC;

/**
 * Created by Administrator on 2016/11/14 0014.
 *
 * 同步时间的类
 *
 * 当前的绝对时间  没有带时区
 *
 */
public class CESyncTime {


    public static final int cmd= CEBC.CMD_SEND_TYPE;
    public static final int data_type=CEBC.K1_DATA_TYPE_SYCN_TIME;
    public static final int dataL=8;


    // 年
    byte year;
    // 月
    byte month;
    // 日
    byte day;
    // 周几
    byte week;
    // 时
    byte hour;
    // 分
    byte min;
    // 秒
    byte sec;
    // 保留字节
    byte reservedChar; // fix to 8 bytes

    public CESyncTime(byte year, byte month, byte day, byte week,
                      byte hour, byte min, byte sec, byte reservedChar) {
        super();
        this.year = year;
        this.month = month;
        this.day = day;
        this.week = week;
        this.hour = hour;
        this.min = min;
        this.sec = sec;
        if ("zh".equals(Locale.getDefault().getLanguage())) {
            this.reservedChar = 1;
        } else {
            this.reservedChar = 2;
        }
    }

    public CESyncTime() {
        Calendar c = Calendar.getInstance();
        year = (byte) (c.get(Calendar.YEAR) - 2000);
        month = (byte) (c.get(Calendar.MONTH) + 1);
        day = (byte) (c.get(Calendar.DAY_OF_MONTH));
        week = (byte) (getRealWeekIndex(c.get(Calendar.DAY_OF_WEEK)));
        hour = (byte) (c.get(Calendar.HOUR_OF_DAY));
        min = (byte) ( (c.get(Calendar.MINUTE))+5);
        sec = (byte) (c.get(Calendar.SECOND));

//        if ("zh".equals(Locale.getDefault().getLanguage())) {
//            this.reservedChar = 1;
//        } else {
//            this.reservedChar = 2;
//        }

        if ("en".equals(Locale.getDefault().getLanguage())) {//西班牙版本
            this.reservedChar = 2;//英文
        } else {
            this.reservedChar = 1;//默认是西班牙语
        }
    }

    private static int getRealWeekIndex(int index) {
        switch (index) {
            case Calendar.SUNDAY:
                return 0;
            case Calendar.MONDAY:
                return 1;
            case Calendar.TUESDAY:
                return 2;
            case Calendar.WEDNESDAY:
                return 3;
            case Calendar.THURSDAY:
                return 4;
            case Calendar.FRIDAY:
                return 5;
            case Calendar.SATURDAY:
                return 6;

        }
        return 0;
    }

    public byte getYear() {
        return year;
    }

    public void setYear(byte year) {
        this.year = year;
    }

    public byte getMonth() {
        return month;
    }

    public void setMonth(byte month) {
        this.month = month;
    }

    public byte getDay() {
        return day;
    }

    public void setDay(byte day) {
        this.day = day;
    }

    public byte getWeek() {
        return week;
    }

    public void setWeek(byte week) {
        this.week = week;
    }

    public byte getHour() {
        return hour;
    }

    public void setHour(byte hour) {
        this.hour = hour;
    }

    public byte getMin() {
        return min;
    }

    public void setMin(byte min) {
        this.min = min;
    }

    public byte getSec() {
        return sec;
    }

    public void setSec(byte sec) {
        this.sec = sec;
    }

    public byte getReservedChar() {
        return reservedChar;
    }

    public void setReservedChar(byte reservedChar) {
        this.reservedChar = reservedChar;
    }

    public byte[] getBytes() {
        byte[] bytes = new byte[8];
        bytes[0] = year;
        bytes[1] = month;
        bytes[2] = day;
        bytes[3] = week;
        bytes[4] = hour;
        bytes[5] = min;
        bytes[6] = sec;
        bytes[7] = reservedChar;
        return bytes;
    }
    public CEDevData toCEDevData(){
        CEDevData data=new CEDevData();
        data.setCmd(cmd);
        data.setDataType(data_type);
        data.setData(getBytes());
        data.setItemL(dataL);
        data.setItemNumber(1);
        return data;
    }

}
